<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        //generator 函数 生成器
    // function* fn(){
    //     console.log(1);
    //     yield 100; //挂起
    //     console.log(2);
    //     yield 200;
    //     console.log(3);
    //     yield 300;
        
    // }
    // let f = fn();
    // console.log(f.next());
    // console.log(f.next());

    // gengerator 改造成 async await
    function* generator(params) {
        console.log('请求开始');
        let res = yield getData();
        console.log('请求结束',res);
        yield "请求结束 了";
    }
    let res = generator();//返回一个迭代器对象
    res.next();//第一个状态执行
    function getData(){
        let xhr = new XMLHttpRequest();
        xhr.open("GET",'my.json', true);
        xhr.onreadystatechange = function(){
            if(xhr.readyState == 4 && xhr.status == 200){
                res.next(xhr.responseText)
            }
        }
        xhr.send();
    }
    </script>
   
</body>
</html>